package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    //部门保存操作
    public void save(Department department) {
        //先保存默认状态
        department.setState(1);
        //然后保存部门对象
        departmentMapper.save(department);
    }

    public void delete(Long id) {
        departmentMapper.delete(id);

    }

    public void update(Department department) {
        //查询原来的状态值,不做修改该状态值
        Department oldDepartment = departmentMapper.get(department.getId());
        department.setState(oldDepartment.getState());
        //保存更新后的部门
        departmentMapper.update(department);
    }

    public Department get(Long id) {
        return departmentMapper.get(id);
    }

    public List<Department> getAll() {

        return departmentMapper.getAll();
    }

    public PageResult<Department> query(ObjectQuery qo) {
        int totalCount = departmentMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<Department>(totalCount, Collections.<Department>emptyList());
        }


        return new PageResult<Department>(totalCount,departmentMapper.queryForList(qo));
    }

    public void changeState(Long id) {
        departmentMapper.changeState(id);
    }

}
